/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uk.ac.cam.juliet.zedcat.client;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Chris
 */
// Wrapping class to fire Events at the correct HotspotEvents class for handling.
public class EventQuantise {

    private final Pipeline mPipeline;
    private Map<java.lang.Integer, HotspotEvents> hotspots;
    private Date lastTimestamp;

    Configuration _conf;

    // Store the configuration and a new mapping from ints to events.
    public EventQuantise(Pipeline p, Configuration conf) {
        mPipeline = p;
        _conf = conf;
        hotspots = new HashMap<java.lang.Integer, HotspotEvents>();
        lastTimestamp = new Date(0);
    }

    // Give this event to the correct handler.
    public void addEvent(Event e) {

        if (e.getRegion() == null) {
          Date eTimestamp = new Date();
          // mPipeline.appendLine("null frame");
          for ( HotspotEvents he : hotspots.values() )
            he.clock(eTimestamp);
        } else {
          Date eTimestamp = e.getRegion().getFrame().getTimestamp();
          // Give the distances to the right hotspots.
          for (HotspotDistance hsd : e.getHotspotDistances()) {
              if (!hotspots.containsKey(hsd.getHotspot().getId()))
                  hotspots.put(hsd.getHotspot().getId(), new HotspotEvents(mPipeline, _conf, hsd.getHotspot().getId()));
              hotspots.get(hsd.getHotspot().getId()).newEvent(eTimestamp, hsd.getDistance(), e.getRegion());
          }
        }
    }

}
